<template>
	<view>
		<view class="np-bottom-view"></view>
		<view class="np-tabbar">
			<block v-for="(item, index) in tabbar" :key="index">
				<block v-if="path === '/pages/list/list'">
					<view class="np-tabbar-item" :class="[activeTplId == item.tplId ? 'np-item-active' : '']" :data-index="index"
					 @click="tabbarSwitch(index)">
						<view>
							<image class="tabbar-icon" :src="item.icon" v-if="activeTplId != item.tplId"></image>
							<image class="tabbar-icon" :src="item.active_icon" v-if="activeTplId == item.tplId"></image>
						</view>
						<view class="np-scale" :style="[activeTplId == item.tplId ? 'color:' + item.active_color : 'color:' + item.color]">{{ item.text }}</view>
					</view>
				</block>
				<view v-else class="np-tabbar-item" :class="[activePath == item.route ? 'np-item-active' : '']" :data-index="index"
				 @click="tabbarSwitch(index)">
					<view>
						<image class="tabbar-icon" :src="item.icon" v-if="activePath != item.route"></image>
						<image class="tabbar-icon" :src="item.active_icon" v-if="activePath == item.route"></image>
					</view>
					<view class="np-scale" :style="[activePath == item.route ? 'color:' + item.active_color : 'color:' + item.color]">{{ item.text }}</view>
				</view>
			</block>
		</view>
	</view>
</template>

<script>
	import base from '@/common/base.js'
	export default {
		name: "npTabbar",
		mixins: [base],
		data() {
			return {
				tabbar: [],
				activePath: '',
				isTabItem: false,
			};
		},
		props: {
			path: {
				type: String,
				default: ''
			},
			activeTplId: {
				type: [Number, String],
				default: 0
			}
		},
		created() {
			this.tabbar = uni.getStorageSync('tabbar');
			if (this.path) {
				this.activePath = this.path;
			}
		},
		watch:{
			globalConfig() {
				if (this.tabbar.length == 0) {
					this.tabbar = uni.getStorageSync('tabbar');
				}
			}
		},
		methods: {
			tabbarSwitch(e) {
				let index = e;
				let activeTab = this.tabbar[index];
				if (activeTab.route === '/pages/list/list') {
					activeTab.url = activeTab.url + '&tab=1';
				}
				if (activeTab.route === '/pages/list/list' && this.activeTplId == activeTab.tplId) {
					return;
				} else if (activeTab.route != '/pages/list/list' && activeTab.route == this.activePath) {
					return;
				}
				let url = activeTab.url;
				uni.redirectTo({
					url: url
				})
			}
		}
	}
</script>

<style>
	.np-tabbar {
		width: 100%;
		position: fixed;
		display: flex;
		align-items: center;
		justify-content: space-between;
		z-index: 99999;
		background: #fff;
		height: 100rpx;
		left: 0;
		bottom: 0;
		padding-bottom: env(safe-area-inset-bottom);
	}

	.np-tabbar::before {
		content: '';
		width: 100%;
		border-top: 1rpx solid #d9d9d9;
		position: absolute;
		top: 0;
		left: 0;
		-webkit-transform: scaleY(0.5);
		transform: scaleY(0.5);
	}

	.np-tabbar-item {
		flex: 1;
		width: 25%;
		display: flex;
		align-items: center;
		flex-direction: column;
		justify-content: space-between;
		font-size: 24rpx;
		color: #666;
		height: 80rpx;
	}

	.np-ptop-4 {
		padding-top: 4rpx;
	}

	.tabbar-icon {
		width: 48rpx;
		height: 48rpx;
	}

	.np-scale {
		font-weight: bold;
		transform: scale(0.8);
		transform-origin: center 100%;
		line-height: 30rpx;
	}

	.np-item-active {
		color: #e41f19 !important;
	}

	.np-safearea-bottom {
		width: 100%;
		height: env(safe-area-inset-bottom);
	}
</style>
